import { IRootState } from '@/store/types'
import { Module } from 'vuex'
import { ISystemState } from './types'

import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData
} from '@/service/main/system/system'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersCount: 0,
      userslist: [],
      rolelist: [],
      roleCount: 0,
      goodsCount: 0,
      goodslist: [],
      menulist: [],
      menuCount: 0
    }
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.userslist = userList
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount
    },
    changeRoleList(state, roleList: any[]) {
      state.rolelist = roleList
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodslist = goodsList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeMenuList(state, menuList: any[]) {
      state.menulist = menuList
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName.toLowerCase()}list`]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName.toLowerCase()}Count`]
      }
    }
  },
  actions: {
    async getPageList({ commit }, payload: any) {
      const pageName = payload.pageName
      const pageUrl = `${pageName.toLowerCase()}/list`

      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageResult.data
      commit(`change${pageName}List`, list)
      commit(`change${pageName}Count`, totalCount)
    },
    async deletePageData(context, payload: any) {
      const pageName = payload.pageName.toLowerCase()
      const id = payload.id
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl)

      context.dispatch('getPageList', {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async createPageDataAction({ dispatch }, payload: any) {
      const pageName = payload?.pageName.toLowerCase()
      const newData = payload?.newData
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)
      //重新请求
      dispatch('getPageList', {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async editPageDataAction({ dispatch }, payload: any) {
      const pageName = payload?.pageName.toLowerCase()
      const { editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)
      //重新请求
      dispatch('getPageList', {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}
export default systemModule
